import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/pages/nft_pages/my_points/points_detail_page.dart';
import 'package:yysk_app/utils/colors.dart';
import 'package:yysk_app/utils/helper.dart';
import 'package:yysk_app/utils/js_tool.dart';
import 'package:yysk_app/widget/custom_app_bar2.dart';
import 'package:yysk_app/widget/image_asset_widget.dart';
import 'package:yysk_app/widget/lazy_network_image.dart';
import 'package:yysk_app/widget/refresh_and_scroll_widget.dart';
import 'package:yysk_app/model/common_model/mine_points_model.dart';
import 'package:yysk_app/net/api/api_url.dart';
import 'package:yysk_app/net/http.dart';

class PointsPage extends StatefulWidget {
  static const path = '/pointsPage';
  const PointsPage({super.key});
  @override
  State<StatefulWidget> createState() => _PointsPage();
}

class _PointsPage extends State<PointsPage> {
  final ScrollController scrollController = ScrollController();

  var pageNum = 1;
  var pageSize = 20;
  final list = RxList<MinePointListModel>([]);

  @override
  void initState() {
    super.initState();
    scrollController.addListener(() {
      if (scrollController.position.pixels ==
          scrollController.position.maxScrollExtent) {
        // getPointListData(loadMore: true);
      }
    });
    _handleRefresh();
  }

  @override
  void dispose() {
    super.dispose();
    scrollController.dispose();
  }

  Size get preferredSize => const Size.fromHeight(kToolbarHeight);

  @override
  Widget build(BuildContext context) {
    double top = MediaQuery.of(context).padding.top;
    return Scaffold(
      body: Column(
        children: [
          Expanded(
            child: Obx(
              () => Stack(
                children: [
                  SizedBox(
                    width: context.width,
                    child: ImageAssetWidget(
                      width: 375.px,
                      height: 155.px,
                      alignment: Alignment.topCenter,
                      imageUrl: 'static/images/tabbar/top_bg.png',
                    ),
                  ),
                  Column(
                    children: [
                      SizedBox(height: top),
                      CustomAppBar2(title: '我的积分', context: context),
                      Expanded(
                        child: Container(
                          padding: EdgeInsets.fromLTRB(15, 10, 15, 0),
                          margin: EdgeInsets.only(top: 56.px),
                          child: RefreshAndScrollWidget(
                            onRefresh: _handleRefresh,
                            scrollController: scrollController,
                            itemBuilder: (item, index) => _listItem(item),
                            itemCount: list.length,
                            items: list,
                          ),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  void _handleRefresh() async {
    // 获取数据
    getPointListData();
  }

  getPointListData({loadMore = false}) {
    // if (loadMore) {
    //   pageNum++;
    // } else {
    //   pageNum = 1;
    // }

    try {
      http.netFetch<List<MinePointListModel>>(
        ApiUrl.pointsPage,
        method: Method.get,
        success: (result) {
          list.value = [];
          List<MinePointListModel> items = result.data ?? [];
          // if (loadMore && items.isEmpty) {
          //   pageNum--;
          // }
          // if (!loadMore) {
          //   list.clear();
          // }
      
          list.addAll(items);
        },
      );
    } catch (e) {
      // if (loadMore) {
      //   pageNum--;
      // }
    }
  }

  _listItem(MinePointListModel item) {
    return GestureDetector(
      onTap: () {
        Get.toNamed(
          '${PointsDetail.path}?pointsId=${item.pointsId}&amount=${item.amount}&pointsName=${item.pointsName}&pointsImageUrl=${item.pointsImageUrl}',
          arguments: {
            'pointsId': item.pointsId,
            'amount': item.amount,
            'pointsName': item.pointsName,
            'pointsImageUrl': item.pointsImageUrl,
          },
        );
      },
      child: Container(
        height: 91.px,
        padding: EdgeInsets.fromLTRB(24.px, 22.px, 24.px, 22.px),
        margin: EdgeInsets.only(bottom: 18.px),
        decoration: BoxDecoration(
          color: Color(0xFFFEFEFE),
          borderRadius: BorderRadius.circular(10.px),
          boxShadow: [
            BoxShadow(
              color: Color(0xFFD3D3D3).withAlpha(50),
              offset: const Offset(0, 28),
              blurRadius: 10,
              spreadRadius: 0,
            ),
          ],
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  children: [
                    LazyImage(
                      imageUrl: '${item.pointsImageUrl ?? ''}',
                      width: 24,
                      height: 24,
                    ),
                    hiSpace(width: 4.px),
                    Text(
                      '${item.pointsName}',
                      style: TextStyle(
                        fontSize: 16.px,
                        color: AppColors.fontColorBlack,
                      ),
                    ),
                  ],
                ),
                Text(
                  '${item.amount ?? 0}',
                  style: TextStyle(
                    fontSize: 14.px,
                    color: AppColors.fontColorGrey,
                  ),
                ),
              ],
            ),
            ImageAssetWidget(
              imageUrl: 'static/images/wallet/right.png',
              width: 20.px,
              height: 20.px,
            ),
          ],
        ),
      ),
    );
  }
}
